package com.example.zxy.constraintlayouttest.activity;

import android.content.Context;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.zxy.constraintlayouttest.R;
import com.example.zxy.constraintlayouttest.adapter.OrderAdapter;
import com.example.zxy.constraintlayouttest.base.BaseActivity;
import com.example.zxy.constraintlayouttest.base.BaseListener;
import com.example.zxy.constraintlayouttest.bean.CommentBean;
import com.example.zxy.constraintlayouttest.model.OrderModel;
import com.example.zxy.constraintlayouttest.util.SharedPreferencesUtils;

import java.util.List;

public class OrderActivity extends BaseActivity {

    private RecyclerView recyclerView;
    Context context;
    private OrderAdapter orderAdapter;
    LinearLayoutManager layoutManager;
    private List<CommentBean> beanList;
    private SharedPreferencesUtils sharedPreferencesUtils;
    private Integer user_id;
    @Override
    public int getLayoutFile() {
        return R.layout.activity_order;
    }

    @Override
    public void initView() {
        context = getContext();
        recyclerView = (RecyclerView)findViewById(R.id.rv);
        sharedPreferencesUtils = new SharedPreferencesUtils(getContext());
    }

    @Override
    public void initEvent() {
        orderAdapter = new OrderAdapter(getContext());
        recyclerView.setAdapter(orderAdapter);
        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);    //提高item性能
        recyclerView.addItemDecoration(new DividerItemDecoration(context,DividerItemDecoration.VERTICAL));
    }

    @Override
    public void initData() {
        user_id = Integer.parseInt(sharedPreferencesUtils.readString("userid"));
        OrderModel orderModel = new OrderModel();
        orderModel.getAllOrders(user_id , baseListener);
    }

    @Override
    public void onClick(View view) {

    }

    BaseListener<List<CommentBean>> baseListener = new BaseListener<List<CommentBean>>() {
        @Override
        public void onResponse(List<CommentBean> commentBeans) {
            if (null != commentBeans){
                beanList = commentBeans;
                orderAdapter.setList(beanList);
            }else {
                showToast("kong");
            }
        }

        @Override
        public void onFail(String msg) {
            showToast(msg);
        }
    };
}
